package com.example.combwx.mvp.view.livestreaming;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.combwx.App;
import com.example.combwx.R;
import com.example.combwx.db.UserUtility;
import com.example.combwx.mvp.base.model.BaseEntity;
import com.example.combwx.mvp.base.view.BaseFragment;
import com.example.combwx.mvp.contract.LREContract;
import com.example.combwx.mvp.di.DaggerLREComponent;
import com.example.combwx.mvp.di.LREModules;
import com.example.combwx.mvp.model.HttpCode;
import com.example.combwx.mvp.model.entity.LiveEntity;
import com.example.combwx.mvp.model.entity.TextBannerEntity;
import com.example.combwx.mvp.model.entity.UserEntity;
import com.example.combwx.mvp.presenter.LREPresenter;
import com.example.combwx.mvp.view.livestreaming.adapter.LiveAdapter;
import com.example.combwx.mvp.view.news.adapter.TextBannerAdapter;
import com.example.combwx.utils.ToasetUtils;
import com.example.combwx.widget.VideoNewsltemDecoration;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.transformer.ZoomOutPageTransformer;

import java.util.HashMap;
import java.util.Map;

public class LiveStreamingFragment extends BaseFragment<LREPresenter>implements
        LREContract.ILREViwe, OnRefreshLoadMoreListener, OnItemClickListener {

    private SmartRefreshLayout live_smart;
    private RecyclerView live_rv;
    private View liveHead,liveHot,liveTitle;
    private Banner<TextBannerEntity.Values, TextBannerAdapter> live_head_banner;
    private LiveAdapter liveAdapter;
    private TextBannerAdapter textBannerAdapter;


    @Override
    public int bindLayout() {
        return R.layout.fragment_live;
    }

    @Override
    public void initView() {
        live_smart = f(R.id.live_smart);
        live_rv = f(R.id.live_rv);
        liveHead = LayoutInflater.from(getContext()).inflate(R.layout.view_live_head,null);
        liveHot = LayoutInflater.from(getContext()).inflate(R.layout.view_live_hot,null);
        liveTitle = LayoutInflater.from(getContext()).inflate(R.layout.view_live_title,null);
        live_head_banner = liveHead.findViewById(R.id.live_head_banner);
        live_smart.setEnableLoadMore(true);
        live_smart.setEnableRefresh(true);
        live_smart.setRefreshHeader(new ClassicsHeader(getContext()));
        live_smart.setRefreshFooter(new ClassicsFooter(getContext()));
        live_smart.setOnRefreshLoadMoreListener(this);
        GridLayoutManager manager = new GridLayoutManager(getContext(),2);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        live_rv.setLayoutManager(manager);
        live_rv.addItemDecoration(new VideoNewsltemDecoration(getContext()));
        liveAdapter = new LiveAdapter();
        liveAdapter.addHeaderView(liveHead);
        liveAdapter.addHeaderView(liveHot);
        liveAdapter.addHeaderView(liveTitle);
        live_rv.setAdapter(liveAdapter);
        liveAdapter.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        Map<String, Object> bannerMap = new HashMap<>();
        bannerMap.put("code", HttpCode.TEXTBANNERCODE);
        Map<String, Object> liveMap = new HashMap<>();
        liveMap.put("code", HttpCode.LIVECODE);
        try {
            UserEntity entity = UserUtility.getInstance().getDaoSession().queryBuilder(UserEntity.class).list().get(0);
            liveMap.put("user_id",entity.user_id);
            p.requestAll(bannerMap,liveMap);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this))
                .build().injectLiveFragment(this);
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof TextBannerEntity){
            TextBannerEntity textBannerEntity = (TextBannerEntity) entity;
            textBannerAdapter = new TextBannerAdapter(textBannerEntity.getValues());
            live_head_banner.setAdapter(textBannerAdapter)
                    .setOrientation(Banner.VERTICAL)
                    .setPageTransformer(new ZoomOutPageTransformer());
        }else if (entity instanceof LiveEntity){
            LiveEntity liveEntity = (LiveEntity) entity;
            liveAdapter.setNewInstance(liveEntity.getValues());
        }
    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {
        LiveEntity liveEntity = (LiveEntity) entity;
        if (liveEntity.getValues() == null){
            return;
        }
        liveAdapter.setNewInstance(liveEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {
        LiveEntity liveEntity = (LiveEntity) entity;
        if (liveEntity.getValues() == null){
            ToasetUtils.showMsg("最后一页");
            return;
        }
        liveAdapter.addData(liveEntity.getValues());
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        Map<String, Object> liveMap = new HashMap<>();
        liveMap.put("code", HttpCode.LIVECODE);
        UserEntity entity = null;
        try {
            entity = UserUtility.getInstance().getDaoSession().queryBuilder(UserEntity.class).list().get(0);
            liveMap.put("user_id",entity.user_id);
            p.requestLoad(liveMap);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        Map<String, Object> liveMap = new HashMap<>();
        liveMap.put("code", HttpCode.LIVECODE);
        UserEntity entity = null;
        try {
            entity = UserUtility.getInstance().getDaoSession().queryBuilder(UserEntity.class).list().get(0);
            liveMap.put("user_id",entity.user_id);
            p.requestRefresh(liveMap);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onStart() {
        super.onStart();
        live_head_banner.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        live_head_banner.stop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        live_head_banner.destroy();
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        LiveEntity.Values entity = (LiveEntity.Values) adapter.getItem(position);
        Bundle bundle = new Bundle();
        bundle.putLong("live_id",entity.live_id);
        bundle.putString("live_url",entity.live_url);
        startPage(bundle,LiveValuesActivity.class);
    }
}
